#include "score_dialog.h"

#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QSpinBox>
#include <QVBoxLayout>

#include "core/music_settings.h"

ScoreDialog::ScoreDialog(MusicSettings *settings, QWidget *parent):
  QDialog(parent),
  settings(settings) {
  setWindowTitle("Partition");
  QGroupBox *scoreBox = new QGroupBox("Partition", this);
  QGridLayout *scoreBoxLayout = new QGridLayout;
  scoreBox->setLayout(scoreBoxLayout);

  QLabel *scoreSizeLabel = new QLabel("Nombre de notes", this);
  scoreBoxLayout->addWidget(scoreSizeLabel, 0, 0);

  scoreSize = new QSpinBox(this);
  scoreSize->setRange(8, 1000);
  scoreSize->setSingleStep(8);
  scoreBoxLayout->addWidget(scoreSize, 0, 1);

  QLabel *nbOctaveSizeLabel = new QLabel("Nombre d'octaves", this);
  scoreBoxLayout->addWidget(nbOctaveSizeLabel, 1, 0);

  nbOctave = new QSpinBox(this);
  nbOctave->setRange(1, 3);
  nbOctave->setSingleStep(1);
  scoreBoxLayout->addWidget(nbOctave, 1, 1);

  // boutons

  QPushButton *okButton = new QPushButton("&Valider", this);
  connect(okButton, SIGNAL(clicked()), this, SLOT(validate()));

  QPushButton *cancelButton = new QPushButton("&Annuler", this);
  connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));

  QHBoxLayout *buttonLayout = new QHBoxLayout;
  buttonLayout->addStretch();
  buttonLayout->addWidget(okButton);
  buttonLayout->addWidget(cancelButton);

  //

  QVBoxLayout *mainLayout = new QVBoxLayout;
  mainLayout->addWidget(scoreBox);
  mainLayout->addLayout(buttonLayout);
  setLayout(mainLayout);

  initialize();
}

/*!
 * Initialise les cases en fonction des paramètres
 */
void ScoreDialog::initialize() {
  scoreSize->setValue(settings->scoreSize);
  nbOctave->setValue(settings->nbOctave);
}

/*!
 * Change les paramètres puis ferme la fenêtre
 */
void ScoreDialog::validate() {
  // multiple de 8 pour avoir une partition complète (8 croches dans une mesure)
  settings->scoreSize = scoreSize->value() + scoreSize->value() % 8;
  settings->nbOctave = nbOctave->value();

  close();
}

